﻿using System;
using WickedFlame.Core.Shapes;
using WickedFlame.Core.State;

namespace WickedFlame.Core.History
{
    public class SizeChangedCommand : HistoryCommand
    {
        VectorShape _shape;
        ShapeMemento origMemento;
        ShapeMemento newMemento;

        public SizeChangedCommand(VectorShape shape, int layerIndex)
            : base(layerIndex)
        {
            Title = "Shape size changed";
            // Keep reference of added object
            _shape = shape;
            if (shape is IStateCapturable)
                origMemento = ((IStateCapturable)_shape).CreateMementoState();
        }

        public void SetNewState()
        {
            if (_shape is IStateCapturable)
                newMemento = ((IStateCapturable)_shape).CreateMementoState();
        }

        public override void Undo(ShapeList list)
        {
            list.UnselectAll();

            if (origMemento != null && _shape is IStateCapturable)
                ((IStateCapturable)_shape).ResetState(origMemento);
        }

        public override void Redo(ShapeList list)
        {
            list.UnselectAll();

            if (newMemento != null && _shape is IStateCapturable)
                ((IStateCapturable)_shape).ResetState(newMemento);
        }
    }
}
